<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="PHP采集知识, wujie">
    <meta name="description" content="无解的技术博客">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>PHP采集知识 | 无解</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    
<meta name="generator" content="Hexo 4.2.0"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">无解</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">无解</div>
        <div class="logo-desc">
            
            无解的技术博客
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/8.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">PHP采集知识</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
        
        position: absolute;
        right: 23.5vw;
        display: block;
    }

    .toc-fixed .toc-link::before{
        position: fixed!important;/*当toc的位置改为fixed时，.toc-link::before也要改为fixed*/
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/PHP/">
                                <span class="chip bg-color">PHP</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Share/" class="post-category">
                                Share
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-05-03
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2020-05-03
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    1.4k
                </div>
                

                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>

        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="序言"><a href="#序言" class="headerlink" title="序言"></a>序言</h2><p>采集文章著名的：火车头、收费、自定义比较差</p>
<p>需要自己编写程序采集目标网站的信息为我所用。</p>
<p>采集必要的知识点：</p>
<ul>
<li>知道PHP发起网络请求的相关函数：</li>
</ul>
<pre class=" language-php"><code class="language-php">file_get_contents
fsockoptn
curl</code></pre>
<ul>
<li>正则表达式/<code>xpath</code></li>
<li>了解一些<code>html</code></li>
<li><code>http</code>相关知识</li>
</ul>
<h2 id="使用curl进行采集"><a href="#使用curl进行采集" class="headerlink" title="使用curl进行采集"></a>使用curl进行采集</h2><p>函数示例：</p>
<pre class=" language-php"><code class="language-php"><span class="token comment" spellcheck="true">/**
 * @param string $url
 * @param array $data
 * @param array $header
 * @return bool|string
 */</span>
<span class="token keyword">function</span> <span class="token function">http_request</span><span class="token punctuation">(</span>string <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">array</span> <span class="token variable">$header</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 初始化</span>
    <span class="token variable">$ch</span> <span class="token operator">=</span> <span class="token function">curl_init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 相关配置</span>
    <span class="token shell-comment comment"># 设置请求的URL地址</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_URL</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token shell-comment comment"># 设置一下执行成功后不直接返回到客户端</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_RETURNTRANSFER</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token shell-comment comment"># 设置超时时间  单位是秒</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_TIMEOUT</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token shell-comment comment"># 不进行证数的检测</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_SSL_VERIFYPEER</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_SSL_VERIFYHOST</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token shell-comment comment"># 伪造一个请求的浏览器型号</span>
    <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_USERAGENT</span><span class="token punctuation">,</span> <span class="token string">'msie'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">// 表示有请求体，是post请求</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token shell-comment comment"># 指明是一个post请求</span>
        <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_POST</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_POSTFIELDS</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">is_string</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token shell-comment comment"># 设置头信息，告诉接受者我们发送的数据类型</span>
            <span class="token function">curl_setopt</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">,</span> <span class="token constant">CURLOPT_HTTPHEADER</span><span class="token punctuation">,</span> <span class="token variable">$header</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">// 执行</span>
    <span class="token variable">$ret</span> <span class="token operator">=</span> <span class="token function">curl_exec</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token shell-comment comment"># 请求的错误码， 为0表示请求正确，大于0则表示请求失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">curl_errno</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">echo</span> <span class="token function">curl_error</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        exit<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">// 关闭请求资源</span>
    <span class="token function">curl_close</span><span class="token punctuation">(</span><span class="token variable">$ch</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token variable">$ret</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="使用该方法来获取一个网站的title信息"><a href="#使用该方法来获取一个网站的title信息" class="headerlink" title="使用该方法来获取一个网站的title信息"></a>使用该方法来获取一个网站的<code>title</code>信息</h2><pre class=" language-php"><code class="language-php"><span class="token delimiter">&lt;?php</span>
<span class="token comment" spellcheck="true">/**
 * Author: Virus
 * Date: 2020/5/3
 * Time: 15:24
 * 采集标题练习1
 * 推荐使用curl，需要手动开启
 */</span>

<span class="token keyword">include</span> <span class="token constant">__DIR__</span><span class="token punctuation">.</span><span class="token string">'/function.php'</span><span class="token punctuation">;</span>

<span class="token variable">$url</span> <span class="token operator">=</span> "https<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//news.ke.com/bj/baike/0033/";</span>

<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// 正则表达式来匹配出 title</span>
<span class="token comment" spellcheck="true">// 修饰符 i 不区分大小写</span>
<span class="token comment" spellcheck="true">// 修饰符 U 禁止贪婪</span>
<span class="token comment" spellcheck="true">// 修饰符 s 忽略换行</span>
<span class="token comment" spellcheck="true">//$preg = '/</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span></span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token markup">&lt;\/title></span><span class="token operator">/</span>iUs'<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 使用一个正则中没有用到的# 来进行定界符，里面的标签就不需要转义</span>
<span class="token variable">$preg</span> <span class="token operator">=</span> <span class="token string">'#&lt;title>(.*)&lt;/title>#iUs'</span><span class="token punctuation">;</span>

<span class="token function">preg_match_all</span><span class="token punctuation">(</span><span class="token variable">$preg</span><span class="token punctuation">,</span> <span class="token variable">$html</span><span class="token punctuation">,</span> <span class="token variable">$arr</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">print_r</span><span class="token punctuation">(</span><span class="token variable">$arr</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<h2 id="xpath路径查询器"><a href="#xpath路径查询器" class="headerlink" title="xpath路径查询器"></a><code>xpath</code>路径查询器</h2><pre class=" language-php"><code class="language-php"><span class="token delimiter">&lt;?php</span>
<span class="token comment" spellcheck="true">/**
 * Author: Virus
 * Date: 2020/5/3
 * Time: 15:24
 * 采集标题练习1
 * 推荐使用curl，需要手动开启
 */</span>

<span class="token keyword">include</span> <span class="token constant">__DIR__</span><span class="token punctuation">.</span><span class="token string">'/function.php'</span><span class="token punctuation">;</span>

<span class="token variable">$url</span> <span class="token operator">=</span> "https<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//news.ke.com/bj/baike/0033/";</span>

<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 声明一个dom对象</span>
<span class="token variable">$dom</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DOMDocument</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 忽略html不严格的格式</span>
<span class="token function">libxml_use_internal_errors</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$dom</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">loadHTML</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 转为xpath对象</span>
<span class="token variable">$xpath</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DOMXPath</span><span class="token punctuation">(</span><span class="token variable">$dom</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">/*
// 查询路径 xpath地址 chrome F12之后右键会有复制项:copy xpath
$query = '/html/head/title';

$nodeList = $xpath->query($query);

foreach ($nodeList as $item) {
    echo $item->nodeValue;
}
*/</span>

<span class="token comment" spellcheck="true">// 查询所有图片</span>
<span class="token comment" spellcheck="true">//$query = '//img/@data-original';</span>
<span class="token variable">$query</span> <span class="token operator">=</span> '<span class="token operator">/</span>html<span class="token operator">/</span>body<span class="token operator">/</span>div<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token operator">/</span>div<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">/</span>div<span class="token operator">/</span>div<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">/</span>div<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token comment" spellcheck="true">//img/@data-original';</span>

<span class="token variable">$nodeList</span> <span class="token operator">=</span> <span class="token variable">$xpath</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$nodeList</span> <span class="token keyword">as</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">echo</span> <span class="token variable">$item</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">nodeValue</span><span class="token punctuation">.</span><span class="token string">"\n"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="QueryList采集器使用"><a href="#QueryList采集器使用" class="headerlink" title="QueryList采集器使用"></a><code>QueryList</code>采集器使用</h2><p>下载：</p>
<pre class=" language-bash"><code class="language-bash">composer require jaeger/querylist:V3.2.1</code></pre>
<p>使用它来实现上面的获取图片、标题、摘要和文章地址：</p>
<pre class=" language-php"><code class="language-php"><span class="token delimiter">&lt;?php</span>

<span class="token keyword">include</span> <span class="token constant">__DIR__</span><span class="token punctuation">.</span><span class="token string">'/function.php'</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// 这个根据个人的地址进行加载</span>
<span class="token keyword">require</span> <span class="token string">'../vendor/autoload.php'</span><span class="token punctuation">;</span>

<span class="token variable">$url</span> <span class="token operator">=</span> "https<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//news.ke.com/bj/baike/0033/";</span>

<span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token package">QL<span class="token punctuation">\</span>QueryList</span><span class="token punctuation">;</span>

<span class="token variable">$dataList</span> <span class="token operator">=</span> QueryList<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">Query</span><span class="token punctuation">(</span>
    <span class="token variable">$html</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span>
        <span class="token comment" spellcheck="true">// 获取图片</span>
        <span class="token string">'img'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.lj-lazy'</span><span class="token punctuation">,</span> <span class="token string">'data-original'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token comment" spellcheck="true">// 获取标题</span>
        <span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text .LOGCLICK'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token comment" spellcheck="true">// 获取摘要</span>
        <span class="token string">'desc'</span>  <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text .summary'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token comment" spellcheck="true">// 获取文章地址</span>
        <span class="token string">'href'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text > a'</span><span class="token punctuation">,</span> <span class="token string">'href'</span><span class="token punctuation">]</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">data</span><span class="token punctuation">;</span>

<span class="token function">var_dump</span><span class="token punctuation">(</span><span class="token variable">$dataList</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<h2 id="使用QueryList采集分页数据后进行入库操作"><a href="#使用QueryList采集分页数据后进行入库操作" class="headerlink" title="使用QueryList采集分页数据后进行入库操作"></a>使用<code>QueryList</code>采集分页数据后进行入库操作</h2><blockquote>
<p>这里的表字段<code>desc</code>当初因为没有使用倒引号 来进行包裹，而导致报错，因为在数据库中<code>desc</code>是关键字</p>
</blockquote>
<pre class=" language-php"><code class="language-php"><span class="token delimiter">&lt;?php</span>
<span class="token comment" spellcheck="true">/**
 * Author: Virus
 * Date: 2020/5/3
 * Time: 21:58
 */</span>

<span class="token comment" spellcheck="true">// 永不超时 不能用nginx和apache，使用命令行</span>
<span class="token function">set_time_limit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">include</span> <span class="token constant">__DIR__</span><span class="token punctuation">.</span><span class="token string">'/function.php'</span><span class="token punctuation">;</span>
<span class="token keyword">require</span> <span class="token string">'../vendor/autoload.php'</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token package">QL<span class="token punctuation">\</span>QueryList</span><span class="token punctuation">;</span>

<span class="token variable">$db</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token string">'mysql:host=localhost;dbname=数据库名称;charset=utf8mb4'</span><span class="token punctuation">,</span> <span class="token string">'用户名'</span><span class="token punctuation">,</span> <span class="token string">'密码'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// 分页测试数据</span>
<span class="token variable">$rang</span> <span class="token operator">=</span> <span class="token function">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rang</span> <span class="token keyword">as</span> <span class="token variable">$page</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$url</span>  <span class="token operator">=</span> 'https<span class="token punctuation">:</span><span class="token comment" spellcheck="true">//news.ke.com/bj/baike/0033/pg'.$page.'/';</span>
    <span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 分析采集内容</span>
    <span class="token variable">$dataList</span> <span class="token operator">=</span> QueryList<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">Query</span><span class="token punctuation">(</span>
        <span class="token variable">$html</span><span class="token punctuation">,</span>
        <span class="token punctuation">[</span>
            <span class="token comment" spellcheck="true">// 获取图片</span>
            <span class="token string">'pic'</span>   <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.lj-lazy'</span><span class="token punctuation">,</span> <span class="token string">'data-original'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">,</span>  <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token comment" spellcheck="true">// 得到扩展名</span>
                    <span class="token variable">$ext</span> <span class="token operator">=</span> <span class="token function">pathinfo</span><span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token constant">PATHINFO_EXTENSION</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">// 生成文件名</span>
                    <span class="token variable">$filename</span> <span class="token operator">=</span> <span class="token function">md5</span><span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token string">'_'</span><span class="token punctuation">.</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token string">'.'</span><span class="token punctuation">.</span><span class="token variable">$ext</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">// 生成的本地路径</span>
                    <span class="token variable">$filepath</span> <span class="token operator">=</span> <span class="token function">dirname</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token string">'/public/uploads/article/'</span><span class="token punctuation">.</span><span class="token variable">$filename</span><span class="token punctuation">;</span>
                    <span class="token function">file_put_contents</span><span class="token punctuation">(</span><span class="token variable">$filepath</span><span class="token punctuation">,</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span> <span class="token string">'/uploads/article/'</span><span class="token punctuation">.</span><span class="token variable">$filename</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true">// 获取标题</span>
            <span class="token string">'title'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text .LOGCLICK'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true">// 获取摘要</span>
            <span class="token string">'desc'</span>  <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text .summary'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token comment" spellcheck="true">// 获取文章地址</span>
            <span class="token string">'url'</span>  <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.item .text > a'</span><span class="token punctuation">,</span> <span class="token string">'href'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">data</span><span class="token punctuation">;</span>

    <span class="token comment" spellcheck="true">// 入库</span>
    <span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$dataList</span> <span class="token keyword">as</span> <span class="token variable">$val</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// 判断标题是否存在，存在则不进行入库</span>

<span class="token comment" spellcheck="true">//        extract($val);</span>
        <span class="token comment" spellcheck="true">// 添加的sql预处理 desc在sql中是关键字需要加上``</span>
        <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string">"insert into articles (`title`,`desc`,`pic`,`url`,`body`) values (?,?,?,?,'')"</span><span class="token punctuation">;</span>
        <span class="token variable">$stmt</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 入库</span>
        <span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token variable">$val</span><span class="token punctuation">[</span><span class="token string">'title'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$val</span><span class="token punctuation">[</span><span class="token string">'desc'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$val</span><span class="token punctuation">[</span><span class="token string">'pic'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$val</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<blockquote>
<p>入库的另外一种操作</p>
</blockquote>
<pre class=" language-php"><code class="language-php"><span class="token comment" spellcheck="true">// 入库</span>
    <span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$dataList</span> <span class="token keyword">as</span> <span class="token variable">$val</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// 判断标题是否存在，存在则不进行入库</span>

        <span class="token function">extract</span><span class="token punctuation">(</span><span class="token variable">$val</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 自动将数组转换成变量</span>
        <span class="token comment" spellcheck="true">// 添加的sql预处理 desc在sql中是关键字需要加上``</span>
        <span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string">"insert into articles (`title`,`desc`,`pic`,`url`,`body`) values (?,?,?,?,'')"</span><span class="token punctuation">;</span>
        <span class="token variable">$stmt</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// 入库</span>
        <span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token variable">$title</span><span class="token punctuation">,</span> <span class="token variable">$desc</span><span class="token punctuation">,</span> <span class="token variable">$pic</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span></code></pre>
<h2 id="抓取内容页面"><a href="#抓取内容页面" class="headerlink" title="抓取内容页面"></a>抓取内容页面</h2><pre class=" language-php"><code class="language-php"><span class="token delimiter">&lt;?php</span>
<span class="token comment" spellcheck="true">/**
 * Author: Virus
 * Date: 2020/5/3
 * Time: 21:58
 */</span>

<span class="token comment" spellcheck="true">// 永不超时 不能用nginx和apache，使用命令行</span>
<span class="token function">set_time_limit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">include</span> <span class="token constant">__DIR__</span><span class="token punctuation">.</span><span class="token string">'/function.php'</span><span class="token punctuation">;</span>
<span class="token keyword">require</span> <span class="token string">'../vendor/autoload.php'</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token package">QL<span class="token punctuation">\</span>QueryList</span><span class="token punctuation">;</span>

<span class="token variable">$db</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PDO</span><span class="token punctuation">(</span><span class="token string">'mysql:host=localhost;dbname=数据库;charset=utf8mb4'</span><span class="token punctuation">,</span> <span class="token string">'用户名'</span><span class="token punctuation">,</span> <span class="token string">'密码'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// 内容页面</span>
<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token string">"select id,url from zfw_articles where body=''"</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// row查询</span>
<span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">fetchAll</span><span class="token punctuation">(</span><span class="token constant">PDO</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token constant">FETCH_ASSOC</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$rows</span> <span class="token keyword">as</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token variable">$id</span>   <span class="token operator">=</span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token variable">$url</span>  <span class="token operator">=</span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'url'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token variable">$html</span> <span class="token operator">=</span> <span class="token function">http_request</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 分析采集内容</span>
    <span class="token variable">$ret</span> <span class="token operator">=</span> QueryList<span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token function">Query</span><span class="token punctuation">(</span><span class="token variable">$html</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'body'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'.bd'</span><span class="token punctuation">,</span> <span class="token string">'html'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">data</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 内容</span>
    <span class="token variable">$body</span> <span class="token operator">=</span> <span class="token variable">$ret</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'body'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 入库</span>

    <span class="token comment" spellcheck="true">// extract($val);</span>
    <span class="token comment" spellcheck="true">// 添加的sql预处理 desc在sql中是关键字需要加上``</span>
    <span class="token variable">$sql</span>  <span class="token operator">=</span> <span class="token string">"update articles set body=? where id=?"</span><span class="token punctuation">;</span>
    <span class="token variable">$stmt</span> <span class="token operator">=</span> <span class="token variable">$db</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token variable">$sql</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// 入库</span>
    <span class="token variable">$stmt</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token variable">$body</span><span class="token punctuation">,</span> <span class="token variable">$id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="实现计划任务进行抓取"><a href="#实现计划任务进行抓取" class="headerlink" title="实现计划任务进行抓取"></a>实现计划任务进行抓取</h2><ul>
<li>先将代码上传到服务器 linux</li>
<li>然后php手动执行php文件</li>
</ul>
<pre class=" language-shell"><code class="language-shell"># crontab
分   时   日   月   星期  脚本

# 每天凌晨1点抓取文章信息 /dev/null 黑洞(所有错误信息进去不显示) 2:失败 1:成功
0    1    *   *     *   php /xxx/xxx/list.php > dev/null 2>&1
# 每天2点抓取文章内容 成功或失败都不要显示(简写)  &>/dev/null
0    2    *   *     *  php /xxx/xxx/body.php &>/dev/null</code></pre>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://wxvirus.gitee.io" rel="external nofollow noreferrer">Virus</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://wxvirus.gitee.io/2020/05/03/PHP%E9%87%87%E9%9B%86%E7%9F%A5%E8%AF%86/">http://wxvirus.gitee.io/2020/05/03/PHP%E9%87%87%E9%9B%86%E7%9F%A5%E8%AF%86/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="http://wxvirus.gitee.io" target="_blank">Virus</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/PHP/">
                                    <span class="chip bg-color">PHP</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/05/05/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E5%92%8C%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/1.jpg" class="responsive-img" alt="mysql实现主从复制和读写分离">
                        
                        <span class="card-title">mysql实现主从复制和读写分离</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            MySQL实现主从复制

在进行curd的同时，主服务器的复制数据库到从服务器中，保证数据不流失


环境介绍
系统环境：centos7.0
客户端连接工具：Xshell
远程文件传输工具：xftp
服务器(虚拟机):
    主服务器：自
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-05-05
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/SQL/" class="post-category">
                                    SQL
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/MySQL/">
                        <span class="chip bg-color">MySQL</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/05/01/DataTables%E7%9A%84%E4%BD%BF%E7%94%A8/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/22.jpg" class="responsive-img" alt="DataTables的使用">
                        
                        <span class="card-title">DataTables的使用</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            分页
使用DataTables插件来实现分页，还带有很多的特殊效果
使用这个需要先引入jQuery的js文件和与之相关的css和js

分页分为：

客户端分页
优点：简单
缺点：数据多了，服务器加载慢、客户端加载慢、会崩溃


服务端分页
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-05-01
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E5%89%8D%E7%AB%AF/" class="post-category">
                                    前端
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/DataTables/">
                        <span class="chip bg-color">DataTables</span>
                    </a>
                    
                    <a href="/tags/jQuery/">
                        <span class="chip bg-color">jQuery</span>
                    </a>
                    
                    <a href="/tags/Laravel/">
                        <span class="chip bg-color">Laravel</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: 无解<br />'
            + '文章作者: Virus<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2020</span>
            <a href="http://wxvirus.gitee.io" target="_blank">Virus</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">34.8k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/xinwangqilin" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1418667580@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1418667580" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1418667580" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
